<?php

namespace App\Models\Shipping;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class ZoneToGeoZone
 * @package App\Models\Shipping
 *
 * @mixin Builder
 */

class ZoneToGeoZone extends Model
{
    protected $table = 'zone_to_geo_zone';
    protected $guarded = [];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function geoZones()
    {
        return $this->hasMany(GeoZone::class, 'geo_zone_id', 'geo_zone_id');
    }

    /**
     * @param array $shipping_data
     * @return \Illuminate\Support\Collection
     */
    public static function getGeoZoneIds(array $shipping_data)
    {
        $zone_to_geo_zones = (new self)->where('country_id', $shipping_data['country_id'])
            ->whereIn('zone_id', [$shipping_data['zone_id'], GeoZone::GEO_ZONE_ID_DEFAULT])
            ->with('geoZones')
            ->get();

        $geo_zone_ids = $zone_to_geo_zones->pluck('geo_zone_id')->prepend(GeoZone::GEO_ZONE_ID_DEFAULT);

        return $geo_zone_ids;
    }
}
